package com.js.mvpdemo.ui.first;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.js.lib_common.base.MvpFragment;
import com.js.lib_common.mvp.model.response.MissionResponse;
import com.js.lib_common.mvp.presenter.MissionListPresenter;
import com.js.lib_common.mvp.view.MissionListView;
import com.js.mvpdemo.R;
import com.js.mvpdemo.adapter.MissionListAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.List;

import butterknife.BindView;

/**
 * 任务列表
 */
public class MissionListFragment extends MvpFragment<MissionListPresenter> implements MissionListView {



    @BindView(R.id.rcy_list)
    RecyclerView rcyList;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private MissionListAdapter mAdapter;
    private int page = 1;

    private  String orderStatus="-1";

    private  static final String ARG_BEAN = "orderStatus";

    public static MissionListFragment newInstance(String orderStatus) {
        Bundle args = new Bundle();
        MissionListFragment fragment = new MissionListFragment();
        args.putString(ARG_BEAN,orderStatus);
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_tab_first_mission_list;
    }


    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
                refreshLayout.autoRefresh(400);//延迟400毫秒后自动刷新
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            orderStatus = bundle.getString(ARG_BEAN);
        }
    }

    @Override
    protected void initView() {


        mAdapter = new MissionListAdapter(null);
        initRecyclerView(rcyList, mAdapter, 1);
        initSmartRefreshLayout(refreshLayout);
        refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                int curPage = page + 1;
                mvpPresenter.getMissionList(curPage,orderStatus);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                mAdapter.getData().clear();
                mAdapter.notifyDataSetChanged();
                mvpPresenter.getMissionList(page,orderStatus);
            }
        });

        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                MissionResponse info =
                        (MissionResponse) adapter.getItem(position);
                                if (info == null)
                                    return;
//                EventBus.getDefault().post(new StartBrotherEvent(MissionDetailFragment.newInstance(info)));
            }
        });

    }

    @Override
    protected MissionListPresenter createPresenter() {
        return new MissionListPresenter(this);
    }


    @Override
    public void onFailed(String message) {
        refreshLayout.finishRefresh(false);//结束刷新（刷新失败）
        refreshLayout.finishLoadMore(false);//结束加载（加载失败）
        toastShow(message);
    }

    @Override
    public void onError(String message) {
        refreshLayout.finishRefresh(false);//结束刷新（刷新失败）
        refreshLayout.finishLoadMore(false);//结束加载（加载失败）
        toastShow(message);
    }

    @Override
    public void showMissionList(int page, boolean isHasMore, List<MissionResponse> data) {
        this.page = page;
        if (data != null && data.size() > 0) {
            for (MissionResponse datum : data) {
                mAdapter.addData(datum);
            }
            mAdapter.notifyDataSetChanged();
        }
        if (isHasMore) {
            refreshLayout.setNoMoreData(false);//恢复没有更多数据的原始状态 1.0.5
        } else {
            refreshLayout.setNoMoreData(true);
        }
        refreshLayout.finishLoadMore();//结束加载
        refreshLayout.finishRefresh();//结束刷新
    }
}